package servlet.hou;

import bean.Hou;
import bean.HygInfo;
import bean.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import service.UserService;
import service.UserServiceImpl;
import utils.DBUtil;
import utils.JwtUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;


//宿管登陆
@WebServlet("/hou/login")
public class HouLoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("=================宿管登录==================");
        //根据输入框标签的name属性值去获取登录名和密码
//        String houName=request.getParameter("houName");
        String password=request.getParameter("password");
        int houId= Integer.parseInt(request.getParameter("houId"));
        //记住密码


        UserService userService=new UserServiceImpl();
        //去查询用户输入的登录名和密码是否正确
        Hou hou =userService.houFindByPasswordAndId(password,houId);

        if(hou ==null){
            //用户输入的名字或密码错误,即没找到该用户，跳转到登录页面，并给予提示信息
            Result result = new Result();
            result.failToLogin();
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);
            System.out.println(str);


        }else{
            //生成token
            Map<String,Object> claim = new HashMap<>();
            claim.put("houId", hou.getHouId());
            claim.put("houName", hou.getHouName());
            claim.put("buildId",hou.getBuildId());
            claim.put("phone",hou.getPhone());
            claim.put("sex",hou.getSex());
            claim.put("age",hou.getAge());
            claim.put("img",hou.getImg());
            claim.put("email",hou.getEmail());
            claim.put("limits",hou.getLimits());
            Result result = new Result();
            result.success(JwtUtils.genToken(claim));
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);
            //说明用户的名字和密码正确，登录成功

            System.out.println("================跳转到宿管主页面=============");
            insertOp("宿管"+hou.getHouName(),hou.getHouId(),"宿管"+hou.getHouName()+"登陆了系统");


        }


    }

    public void insertOp(String s, Integer houId, String detail) {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Connection connection=null;
        HygInfo hygInfo = new HygInfo();
        try {
            connection= DBUtil.getConnection();
            String sql="insert into op(op_name,do_id,op_detail) values(?,?,?)";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,s);
            preparedStatement.setInt(2,houId);
            preparedStatement.setString(3,detail);
            int result =preparedStatement.executeUpdate();


        } catch (SQLException e) {
            System.out.println(e);

        }

        DBUtil.close(connection,preparedStatement,resultSet);

        Result result = new Result();
        result.success(1);

    }
}
